<?php
namespace file\localfile;

use think\facade\Filesystem;
use think\exception\FileException;

class Localfile
{
    /**
     * 本地上传
     * @param $file
     * @param string $path
     * @return array
     */
    public static function uploadFile($file, $path = "storage")
    {
        try {
            $filename = Filesystem::disk('public')->putFile($path, $file);
            $filename_ret = str_replace("\\","/", $filename);
        } catch(FileException $e) {
            return ['status' => false, 'msg' => $e->getMessage()];
        }

        return ['status' => true, 'href' => $filename_ret];
    }

    /**
     * 删除文件/多个文件 传入文件路径数组形式
     * $file_paths = [];
     * $file_paths[] = "文件路径1";
     * $file_paths[] = "文件路径2";
     *
     * @param array $file_paths
     * @return bool
     */
    public static function deleteFile($file_paths = [])
    {
        if(!is_array($file_paths)) {
            return false;
        }
        foreach ($file_paths as $file_path) {
            $path = public_path() ."/". $file_path;
            if (file_exists($path)) unlink($path);
        }
        return true;
    }
}